package com.pdsu.graduation_design.views;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.Toast;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.graduation_design.MainActivity;
import com.pdsu.graduation_design.R;
import com.pdsu.graduation_design.domain.User;
import com.pdsu.graduation_design.utils.JsonUtil;
import com.pdsu.graduation_design.utils.MyDBOpenHelper;
import com.pdsu.graduation_design.utils.Result;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class SplashActivity extends AppCompatActivity {

    private MyDBOpenHelper myDBOpenHelper;
    private SQLiteDatabase db;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        init();
        bind();
        //创建子线程
        Thread mThread=new Thread(){
            @Override
            public void run() {
                super.run();
                try {
                    sleep(1000);//使程序休眠3秒
                    Intent intent;
                    //查询本地用户id 用户名 token
                    //1. 查询本地记录
                       Cursor cursor= db.query("user02",null,null,null,null,null,null);

                       //有数据
                        if (cursor.moveToFirst()){
                            //验证token
                            //请求携带的参数
                            @SuppressLint("Range") String user_token = cursor.getString(cursor.getColumnIndex("usertoken"));
                            @SuppressLint("Range") String phone = cursor.getString(cursor.getColumnIndex("phone"));
                            @SuppressLint("Range") String username = cursor.getString(cursor.getColumnIndex("username"));
                            @SuppressLint("Range") int uid = Integer.parseInt(cursor.getString(cursor.getColumnIndex("uid")));
                            RequestParams requestParams=new RequestParams();
                            requestParams.setUri("http://10.0.2.2:8088/user/logincheck");
                            requestParams.addBodyParameter("token",user_token);
                            requestParams.addBodyParameter("phone",phone);
                            requestParams.addBodyParameter("uid",uid+"");
                            User u=new User();
                            u.setPhone(phone);
                            u.setUsername(username);
                            u.setToken(user_token);
                            u.setUid(uid);
                            //**********
                            x.http().post(requestParams, new Callback.CommonCallback<String>() {
                                @Override
                                public void onSuccess(String result) {

                                    try {
                                        Result r= JsonUtil.JsonToObj(result,Result.class);
                                        if (r.getCode().equals("200")){
                                            //检查成功 ， 将本地数据填充至user

                                            /**
                                             * 更新用户token
                                             */
                                            u.setToken(r.getData().toString());
                                            MainActivity.user_admin=u;
                                            db.execSQL("update user02 set usertoken = ?",new Object[]{u.getToken()});
                                            //
                                             Intent intent2=new Intent(getApplicationContext(), MainActivity.class);
                                            startActivity(intent2);
                                            finish();
                                        }
                                        else {
                                            db.execSQL("delete from user02");
                                            Toast.makeText(context, "登录失效，请重新登录", Toast.LENGTH_SHORT).show();
                                            System.out.println("跳转");
                                           Intent intent3=new Intent(getApplicationContext(), UserLoginActivity.class);
                                            startActivity(intent3);
                                            finish();
                                        }

                                    } catch (JsonProcessingException e) {
                                        e.printStackTrace();
                                    }


                                    System.out.println(result);

                                }

                                @Override
                                public void onError(Throwable ex, boolean isOnCallback) {

                                }

                                @Override
                                public void onCancelled(CancelledException cex) {

                                }

                                @Override
                                public void onFinished() {

                                }
                            });
                            //**********
//                            intent=new Intent(getApplicationContext(), MainActivity.class);
                        }
//
                    else{
                            Intent intent5=new Intent(getApplicationContext(), UserLoginActivity.class);
                            startActivity(intent5);
                            finish();
                        }
                      // 无 跳转到登录页面


                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        };
        mThread.start();
    }

    void init(){

        context=this;

        myDBOpenHelper=new MyDBOpenHelper(context,"my.db",null,3);

        db=myDBOpenHelper.getWritableDatabase();


    }
    void bind(){

    }
}